﻿/*
 * zlog_unittest.cxx
 *
 *  Created on: 2015年4月13日
 *      Author: Fifi Lyu
 */

#include <gtest/gtest.h>
#include "zpsoe/zfilesys.h"
#include "zpsoe/zlog.h"
#include <vector>

using zpsoe::zlog::G_LOG_CONFIG;
using zpsoe::zfilesys::read_file;
using zpsoe::zfilesys::write_file;
using std::vector;

void get_value(
        const string &msg, const int col_num, int &match_col, char *msg_buff) {
    int time_year_ = 0;
    int time_month_ = 0;
    int time_day_ = 0;
    int time_hour_ = 0;
    int time_minute_ = 0;
    int time_second_ = 0;
    int pid_ = 0;
    char file_[200];

    memset(msg_buff, 0, 50);

    if (col_num == 9)
        match_col = sscanf(
                msg.c_str(),
                "[%d-%d-%d %d:%d:%d] [%d] %s %s",
                &time_year_,
                &time_month_,
                &time_day_,
                &time_hour_,
                &time_minute_,
                &time_second_,
                &pid_,
                file_,
                msg_buff);
    else
        match_col = sscanf(
                msg.c_str(),
                "[%d-%d-%d %d:%d:%d] [%d] %s",
                &time_year_,
                &time_month_,
                &time_day_,
                &time_hour_,
                &time_minute_,
                &time_second_,
                &pid_,
                msg_buff);
}

void check_log() {
    vector<string> v_;
    int ret_ = 0;

    read_file(G_LOG_CONFIG.path, v_);
    EXPECT_EQ(6, v_.size());

    int match_col_ = 0;
    char msg_buff[50];

    get_value(v_[0], 9, match_col_, msg_buff);
    EXPECT_EQ(9, match_col_);
    EXPECT_STREQ("LOG_TRACE", msg_buff);

    get_value(v_[1], 8, match_col_, msg_buff);
    EXPECT_EQ(8, match_col_);
    EXPECT_STREQ("LOG_DEBUG", msg_buff);

    get_value(v_[2], 8, match_col_, msg_buff);
    EXPECT_EQ(8, match_col_);
    EXPECT_STREQ("LOG_INFO", msg_buff);

    get_value(v_[3], 8, match_col_, msg_buff);
    EXPECT_EQ(8, match_col_);
    EXPECT_STREQ("LOG_WARN", msg_buff);

    get_value(v_[4], 8, match_col_, msg_buff);
    EXPECT_EQ(8, match_col_);
    EXPECT_STREQ("LOG_ERROR", msg_buff);

    get_value(v_[5], 8, match_col_, msg_buff);
    EXPECT_EQ(8, match_col_);
    EXPECT_STREQ("LOG_FATAL", msg_buff);
}

TEST(ZlogTest, print_log) {
    G_LOG_CONFIG.level = LOG_TRACE;
    G_LOG_CONFIG.output_to = LOS_FILE;

    write_file(G_LOG_CONFIG.path, "");

    //[2015-04-13 14:28:05] [16894] /home/fifilyu/workspace/zpsoe/test/zlog_unittest.cxx:24 LOG_TRACE
    //[2015-04-13 14:28:05] [16894] LOG_DEBUG
    //[2015-04-13 14:28:05] [16894] LOG_INFO
    //[2015-04-13 14:28:05] [16894] LOG_WARN
    //[2015-04-13 14:28:05] [16894] LOG_ERROR
    //[2015-04-13 14:28:05] [16894] LOG_FATAL
    print_log(LOG_TRACE, "LOG_TRACE", 0);
    print_log(LOG_DEBUG, "LOG_DEBUG", 1);
    print_log(LOG_INFO,  "LOG_INFO",  2);
    print_log(LOG_WARN,  "LOG_WARN",  3);
    print_log(LOG_ERROR, "LOG_ERROR", 4);
    print_log(LOG_FATAL, "LOG_FATAL", 5);
    print_log(LOG_OFF,   "LOG_OFF",   6);

    check_log();
}


